package fr.paris.lutece.plugins.customisation.dnd.update;

import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import fr.paris.lutece.plugins.customisation.business.portlet.PersonalPortlet;
import fr.paris.lutece.plugins.customisation.business.portlet.PersonalPortletHome;
import fr.paris.lutece.plugins.customisation.business.tab.CustomisationTab;
import fr.paris.lutece.plugins.customisation.business.tab.CustomisationTabHome;

/**
 * Singleton class, responsible for updating portlets positions and columns
 * sizes
 * 
 * @author team LutecePP[ENSEIRB]
 */

class PositionUpdateManager {

	private static final String UPDATE_ACTION = "update_action";
	private static final String UPDATE_PORTLETS = "portlets";
	private static final String UPDATE_COLUMNS = "columns";
	private static final String TAB_ID_PREFIX = "tab_id";
	private static final String PORTLET_POSITION_PREFIX = "dnd_";
	private static final String FIRST_COLUMN_SIZE_PREFIX = "w1";
	private static final String SECOND_COLUMN_SIZE_PREFIX = "w2";
	private static final String THIRD_COLUMN_SIZE_PREFIX = "w3";

	private static PositionUpdateManager uniqueInstance;

	private PositionUpdateManager() {
	}

	static PositionUpdateManager getPositionUpdateManager() {
		if (uniqueInstance == null)
			uniqueInstance = new PositionUpdateManager();
		return uniqueInstance;
	}

	/**
	 * Dispatch updating between column size requests and portlet positions requests
	 * */
	void update(HttpServletRequest request) {

		if (request.getParameter(UPDATE_ACTION).equals(UPDATE_PORTLETS))
			parsePortletPositionUpdateRequest(request);

		if (request.getParameter(UPDATE_ACTION).equals(UPDATE_COLUMNS))
			parseColumnSizeUpdateRequest(request);
		
	}

	/**
	 * Parse the portlets positions from the request
	 */
	private void parsePortletPositionUpdateRequest(HttpServletRequest request) {

		int tabId = Integer.parseInt(request.getParameter(TAB_ID_PREFIX));

		Enumeration<String> parameterNames = request.getParameterNames();

		/* parse the personal portlets positions */
		while (parameterNames.hasMoreElements()) {

			String parameterName = parameterNames.nextElement();

			if (parameterName.startsWith(PORTLET_POSITION_PREFIX)) {

				System.out.println(parameterName);

				int portletId = Integer.parseInt(parameterName.split("_")[1]);

				// get the corresponding business object
				PersonalPortlet personalPortlet = PersonalPortletHome
						.findByPrimaryKeyTabKey(portletId, tabId);

				// get the new positions : column and order (eg "2,4")
				String newPositions = request.getParameter(parameterName);

				// split column and order
				String[] columnAndOrder = newPositions.split(",");

				int newColumn = Integer.parseInt(columnAndOrder[0]);
				int newOrder = Integer.parseInt(columnAndOrder[1]);

				// launch update
				updatePersonalPortlet(personalPortlet, newColumn, newOrder);
			}
		}
	}

	/**
	 * Update the portlet position in database
	 */
	private void updatePersonalPortlet(PersonalPortlet personalPortlet,
			int newColumn, int newOrder) {

		personalPortlet.setOrder(newOrder);
		personalPortlet.setColumn(newColumn);

		PersonalPortletHome.update(personalPortlet);

		System.out.println("UPDATE OK : " + personalPortlet.getName()
				+ " COL : " + newColumn + " ORD : " + newOrder);
	}
	
	/**
	 * Parse the columns sizes from the request
	 */
	private void parseColumnSizeUpdateRequest(HttpServletRequest request) {

		int tabId = Integer.parseInt(request.getParameter(TAB_ID_PREFIX));

		/* update column sizes */
		updateColumnSizes(tabId, Integer.parseInt(request.getParameter(
				FIRST_COLUMN_SIZE_PREFIX).split("px")[0]), Integer
				.parseInt(request.getParameter(SECOND_COLUMN_SIZE_PREFIX)
						.split("px")[0]), Integer.parseInt(request
				.getParameter(THIRD_COLUMN_SIZE_PREFIX).split("px")[0]));
	}

	/**
	 * Update the columns sizes
	 */
	private void updateColumnSizes(int tabId, int width1, int width2, int width3) {

		System.out.println(" UPDATE COLUMN SIZE : " + width1 + "," + width2
				+ "," + width3);

		CustomisationTab tab = CustomisationTabHome.findByPrimaryKey(tabId);

		CustomisationTabHome.update(tab.getIdTab(), tab.getNameTab(), tab
				.getUserId(), tab.getOrderTab(), tab.getIdTemplate(), tab
				.getCodeTheme(), width1, width2, width3);
	}

}
